/**
 * 申请代理 js 脚本
 * Created by hejx on 2017/6/28.
 */
app.controller('applyProxyController', function($scope,$http) {

    $scope.URL = {
        GET_IDENTIFYCODE_URL:"/getIdentifyCode",
        SUBMIT_URL:"/mp/applyProxy"
    };

    $scope.param = {
        playerId:playerId,
        phone:"",
        // openid:openid,
        realname:"",
        identifyCode:""
    };

    $scope.countdown = 60;
    $scope.setTime = function(elem){
        if ($scope.countdown == 0) {
            elem.removeAttribute("disabled");
            elem.innerHTML = "获取短信验证码";
            $scope.countdown = 60;
        } else {
            elem.setAttribute("disabled", true);
            elem.innerHTML = "重新发送(" + $scope.countdown + ")";
            $scope.countdown--;
            setTimeout(function() {
                $scope.setTime(elem);
            },1000)
        }
    };

    //校验参数 return boolean
    $scope.validateParam = function(){
        if(!validatemobile($scope.param.phone)){
            layer.msg(common.ERR_MSG.PHONE_ERROR_MSG);
            return true;
        }
        if(isNull($scope.param.identifyCode)){
            layer.msg("请输入短信验证码");
            return true;
        }
        return false;
    }

    //save操作
    $scope.save = function(){

        if($scope.validateParam()){
            return false;
        }

        $('#subBtn')[0].disabled = true;
        //提交数据
        $http({method : 'POST', data:$scope.param, url : $scope.URL.SUBMIT_URL})
            .success(function(data,status,headers,config){
                $('#subBtn')[0].disabled = false;
                if(data.code==0){
                    layer.alert("申请成功!", {
                        icon: 6,
                        skin: 'layer-ext-moon',
                        end:function(){
                            //申请完成 刷新页面
                            window.location.reload();
                        }
                    })
                }else{
                    layer.alert(data.msg, {
                        icon: 5,
                        skin: 'layer-ext-moon'
                    })
                }
            }).error(function(data,status,headers,config){
            $('#subBtn')[0].disabled = false;
            console.log('error..........');
            console.log(data);
        });
    }

    //获取短信验证码
    $scope.getIdentifyCode = function($event){
        var elem = $event.target;
        if(!validatemobile($scope.param.phone)){
            layer.msg(common.ERR_MSG.PHONE_ERROR_MSG);
            return false;
        }
        //发送短信
        $http.get($scope.URL.GET_IDENTIFYCODE_URL,{params:{phone:$scope.param.phone,type:0}}).success(function(data,status,headers,config){
            if(data.code==0){
                layer.msg("短信已发送");
                $scope.setTime(elem);
            }
        }).error(function(data,status,headers,config){
            console.log('error..........');
            console.log(data);
        })

    };

    $scope.playerMsg = null; //错误信息
    $scope.playerInfo = null; //玩家信息

    //查询玩家信息模块
    $scope.serchPlayerInfo = {
        serch : function(){

            if(isNull($scope.param.playerId)){
                layer.msg("请输入玩家ID");
                return false;
            }

            $.ajax({
                url : '/admin/gm/serchRoleInfo',
                type:'GET', //GET
                async:false,    //或false,是否异步
                data:{
                    uid:$scope.param.playerId
                },
                timeout:5000,    //超时时间
                dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                success:function(data,textStatus,jqXHR){
                    if(data.code == 1){
                        //查询到指定ID的角色信息
                        $scope.playerMsg = null;
                        $scope.serchPlayerInfo.playerInfo(data.data);
                    }else{
                        $scope.playerMsg = "没有查询到ID为 "+$scope.param.playerId+" 的玩家信息";
                        $scope.playerInfo = null;
                    }
                },
                error:function(xhr,textStatus){
                    console.error('错误')
                    console.error(xhr)
                    console.error(textStatus)
                }
            })
        },
        playerInfo : function(role){
            console.log(role);
            $scope.playerInfo = "玩家昵称:" + role.name;
        }
    };


});